package com.coc.util;

import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
import com.schooner.MemCached.MemcachedItem;

import java.security.MessageDigest;
import java.util.Date;

public class MemCacheUtil {
	//127.0.0.1:1234
	private static String[] serverlist = {"219.239.223.50:11211"};
	private static SockIOPool pool = null;
	private static MemCachedClient mc = null;
	private static boolean isExt = false;

	public static void setServerlist(String[] serverlists) {
		serverlist = serverlists;
	}

	public static void setIsExt(boolean ext) {
		isExt = ext;
	}

	public static String getValue(String key) {
		if ((mc == null) || (pool == null)) {
			pool = SockIOPool.getInstance();
			pool.setServers(serverlist);
			pool.initialize();
			mc = new MemCachedClient();
			mc.setExt(isExt);
		}
		if(mc.get(key)==null){
			return null;
		}
		String values = (String) mc.get(key);
		if (values.equals("null")) {
			return null;
		}
		return values;
	}

	public static int setValue(String key, String value) {
		if ((mc == null) || (pool == null)) {
			pool = SockIOPool.getInstance();
			pool.setServers(serverlist);
			pool.initialize();
			mc = new MemCachedClient();
			mc.setExt(isExt);
		}
		return mc.set(key, value);
	}

	public static int setValue(String key, String value, Date exprie) {
		if ((mc == null) || (pool == null)) {
			pool = SockIOPool.getInstance();
			pool.setServers(serverlist);
			pool.initialize();
			mc = new MemCachedClient();
			mc.setExt(isExt);
		}
		return mc.set(key, value, exprie);
	}

	public static int addValue(String key, String value) {
		if ((mc == null) || (pool == null)) {
			pool = SockIOPool.getInstance();
			pool.setServers(serverlist);
			pool.initialize();
			mc = new MemCachedClient();
			mc.setExt(isExt);
		}
		return mc.add(key, value);
	}
	public static int setObject(String key, Object ob,Date expiry ) {
		if ((mc == null) || (pool == null)) {
			pool = SockIOPool.getInstance();
			pool.setServers(serverlist);
			pool.initialize();
			mc = new MemCachedClient();
			mc.setExt(isExt);
		}
		return mc.set(key,ob,expiry);
	}
	public static int setObject(String key, Object ob) {
		if ((mc == null) || (pool == null)) {
			pool = SockIOPool.getInstance();
			pool.setServers(serverlist);
			pool.initialize();
			mc = new MemCachedClient();
			mc.setExt(isExt);
		}
		return mc.set(key,ob,new Date(0L));
	}

	public static int addObject(String key, Object ob) {
		if ((mc == null) || (pool == null)) {
			pool = SockIOPool.getInstance();
			pool.setServers(serverlist);
			pool.initialize();
			mc = new MemCachedClient();
			mc.setExt(isExt);
		}
		return mc.add(key, ob,new Date(0L));
	}

	public static Object getObject(String key) {
		if ((mc == null) || (pool == null)) {
			pool = SockIOPool.getInstance();
			pool.setServers(serverlist);
			pool.initialize();
			mc = new MemCachedClient();
			mc.setExt(isExt);
		}
		return mc.get(key);
	}

	public static MemcachedItem getsObject(String key) {
		if ((mc == null) || (pool == null)) {
			pool = SockIOPool.getInstance();
			pool.setServers(serverlist);
			pool.initialize();
			mc = new MemCachedClient();
			mc.setExt(isExt);
		}
		return mc.gets(key);
	}
	public static void casObject(String key,Object object,long casUnique) throws MemException {
		if ((mc == null) || (pool == null)) {
			pool = SockIOPool.getInstance();
			pool.setServers(serverlist);
			pool.initialize();
			mc = new MemCachedClient();
			mc.setExt(isExt);
		}
		int res= mc.cas(key, object,new Date(System.currentTimeMillis()+2592000000L),casUnique);
		if(res!=1){
			throw new MemException("memcached cas error,cas key:"+key,res);
		}
	}
	public static int delValue(String key) {
		if ((mc == null) || (pool == null)) {
			pool = SockIOPool.getInstance();
			pool.setServers(serverlist);
			pool.initialize();
			mc = new MemCachedClient();
			mc.setExt(isExt);
		}
		return mc.delete(key);
	}

	public static int appendValue(String key, String value) {
		if ((mc == null) || (pool == null)) {
			pool = SockIOPool.getInstance();
			pool.setServers(serverlist);
			pool.initialize();
			mc = new MemCachedClient();
			mc.setExt(isExt);
		}
		return mc.append(key, value);
	}

	public static int replaceValue(String key, String value) {
		if ((mc == null) || (pool == null)) {
			pool = SockIOPool.getInstance();
			pool.setServers(serverlist);
			pool.initialize();
			mc = new MemCachedClient();
			mc.setExt(isExt);
		}
		return mc.replace(key, value);
	}

	public static boolean isSceneLock(int uid) {
		String lock = getValue(uid + "_lock_scene");
		if ((lock != null) && (lock.equals("1"))) {
			return true;
		}
		return false;
	}

	public static int LockScene(int uid) {
		return addValue(uid + "_lock_scene", "1");
	}

	public static int releaseSceneLock(int uid) {
		return delValue(uid + "_lock_scene");
	}

	public static int setSession(int uid, String token) {
		return setValue(uid + "_tk", token,new Date(System.currentTimeMillis()+1800000));
	}

	public static String getSession(int uid) {
		String token = getValue(uid + "_tk");
		//读取过数据就强制更新过期日期
		//不熟悉memcached,不懂读取会不会重置过期时间,先强制设置
		if(token!=null){
			setValue(uid + "_tk", token,new Date(System.currentTimeMillis()+1800000));
		}
		return token;
	}

	public static String MD5digest(String message) {
		char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
				'a', 'b', 'c', 'd', 'e', 'f' };
		try {
			byte[] strTemp = message.getBytes();
			MessageDigest mdTemp = MessageDigest.getInstance("MD5");
			mdTemp.update(strTemp);
			byte[] md = mdTemp.digest();
			int j = md.length;
			char[] str = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				str[(k++)] = hexDigits[(byte0 >>> 4 & 0xF)];
				str[(k++)] = hexDigits[(byte0 & 0xF)];
			}
			return new String(str);
		} catch (Exception e) {
		}
		return null;
	}
}

/*
 * Location: C:\Work\WorkSpace\classes\ Qualified Name:
 * com.coc.util.MemCacheUtil JD-Core Version: 0.6.2
 */